package com.linkwedo.saas.users;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.linkwedo.saas.domain.member.User;

public class UserDao extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
	.getLogger(UserDao.class);
	// property constants


	public void save(User transientInstance) {
		log.debug("saving User instance");
		Session curSession = getSession();
		try {
			curSession.save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void delete(User persistentInstance) {
		log.debug("deleting com.huayanginfo.saas.domain.member.User instance");
		Session curSession = getSession();
		try {
			Transaction tx = curSession.beginTransaction();
			curSession.delete(persistentInstance);
			log.debug("delete successful");
			tx.commit();
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public User findById(java.lang.Integer id) {
		log.debug("getting User instance with id: " + id);
		Session curSession = getSession();
		try {
			User instance = (User) curSession.get(
					"User", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByExample(User instance) {
		log.debug("finding User instance by example");
		Session curSession = getSession();
		try {
			List results = curSession.createCriteria("User").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding User instance with property: " + propertyName
				+ ", value: " + value);
		Session curSession = getSession();
		try {
			String queryString = "from User as model where model."
					+ propertyName + "= ?";
			Query queryObject = curSession.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}
	
	

	public List findAll() {
		log.debug("finding all User instances");
		Session curSession = getSession();
		try {
			String queryString = "from User";
			Query queryObject = curSession.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public User merge(User detachedInstance) {
		log.debug("merging User instance");
		Session curSession = getSession();
		try {
			User result = (User) curSession.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void attachDirty(User instance) {
		log.debug("attaching dirty User instance");
		Session curSession = getSession();
		try {
			curSession.saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}

	public void attachClean(User instance) {
		log.debug("attaching clean User instance");
		Session curSession = getSession();
		try {
			curSession.lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}finally{
			curSession.close();
		}
	}
	
}
